﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DARE.Save.DataStructures
{
    public class CGameObjectMgrData : ISaveData
    {
        public List<CGameObjectData> m_gameObjects = new List<CGameObjectData>();
        public List<CGameObjectData> m_instances = new List<CGameObjectData>();


        public void FillDataStructure(object gameObjectMgr)
        {
            CGameObjectMgr gom = gameObjectMgr as CGameObjectMgr;
            foreach (CGameObject go in gom.GetAllGameObjectTemplates())
            {
                CGameObjectData goData = CDataStructuresMgr.Instance.SaveItem(go) as CGameObjectData;
                m_gameObjects.Add(goData);
            }

            foreach (CGameObject go in gom.GetAllInstances())
            {
                CGameObjectData goData = CDataStructuresMgr.Instance.SaveItem(go) as CGameObjectData;
                m_instances.Add(goData);
            }
        }

        public void FillFromDataStructure(out object gameObjectMgr, params object[] paramsData)
        {
            foreach (CGameObjectData data in m_gameObjects)
            {
                CGameObject obj = data.gameObject;
                data.FillFromDataStructure(ref obj, paramsData[0] as CRender3D, paramsData[1] as CHUDSystem);
                (paramsData[0] as CRender3D).GameObjectMgr.RegisterGameObject(obj);
            }

            List<CGameObjectData> goData = new List<CGameObjectData>();
            foreach (CGameObjectData data in m_instances)
            {
                uint id = data.gameObject.ID;
                (paramsData[0] as CRender3D).GameObjectMgr.RegisterInstantiation(data.gameObject);
                data.gameObject.ID = id;
                goData.Add(data);
            }

            foreach (CGameObjectData data in goData)
            {
                CGameObject obj = (paramsData[0] as CRender3D).GameObjectMgr.FindInstance(data.gameObject.ID);
                data.FillFromDataStructure(ref obj, paramsData[0] as CRender3D, paramsData[1] as CHUDSystem);
            }
            gameObjectMgr = (paramsData[0] as CRender3D).GameObjectMgr;

        }
    }
}
